# Sample Plugin: Container Tag
#

package MT::Plugin::MyPlugin06;

use strict;
use MT;
use MT::Template::Context;
use vars qw($VERSION);
$VERSION = '0.1';

@MT::Plugin::MyPlugin06::ISA = qw(MT::Plugin);

my $plugin = new MT::Plugin::MyPlugin06({
    name => '<MT_TRANS phrase=\'_PLUGIN_NAME\'>',
    version => $VERSION,
    description => '<MT_TRANS phrase=\'_PLUGIN_DESCRIPTION\'>',
    author_name => '<MT_TRANS phrase=\'_PLUGIN_AUTHOR\'>',
    author_link => 'http://www.example.com/about/',
    doc_link => 'http://www.example.com/docs/',
    l10n_class => 'MyPlugin06::L10N',
});

MT->add_plugin($plugin);

MT::Template::Context->add_container_tag(Templates => \&templates);
MT::Template::Context->add_tag(TemplateName => \&template_name);

sub templates {
    my ($ctx, $args, $cond) = @_;
    my $tokens = $ctx->stash('tokens');
    my $builder = $ctx->stash('builder');
    my $iter = MT::Template->load_iter({ blog_id => $ctx->stash('blog_id') },
                                       { 'sort' => 'name' });
    my $res = '';
    while (my $tmpl = $iter->()) {
        local $ctx->{__stash}{'tmpl'} = $tmpl;
        defined(my $out = $builder->build($ctx, $tokens, $cond)) or
                return $ctx->error( $builder->errstr );
        $res .= $out;
    }
    $res;
}

sub template_name {
    my ($ctx, $args, $cond) = @_;
    my $tmpl = $ctx->stash('tmpl')
        or return _no_template_error($ctx, 'MT'.$ctx->stash('tag'));
    $tmpl->name;
}

sub _no_template_error {
    my ($ctx, $tag) = @_;
    $ctx->error(MT->translate('You used an [_1] tag outside of the proper context.', $tag));
}

1;
